package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.AuthorityDao;
import mil.af.amc.adpetracker.model.Authority;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * Hibernate DAO Implementation for Authority objects.
 *
 * Most methods that would normally modify an object are overriden to prevent the three basic roles from being altered
 *
 * @author TSgt Jason Ferguson
 */

public class HibernateAuthorityDao extends HibernateGenericDao<Authority> implements AuthorityDao {

    public HibernateAuthorityDao() {
        super(Authority.class);
    }

    @Override
    public void delete(Authority persistentObject) {

    }

    @Override
    public void saveOrUpdate(Authority object) {

    }

    @Override
    public void save(Authority object) {

    }

    @Override
    public Integer add(Authority object) {

        return null;
    }

    @Override
    public Authority merge(Authority object) {

        return null;
    }

    @SuppressWarnings({"unchecked"})
    public Authority getByName(String name) {

        DetachedCriteria crit = DetachedCriteria.forClass(Authority.class);
        crit.add(Restrictions.ilike("authority", name));
        List<Authority> results = (List<Authority>) getHibernateTemplate().findByCriteria(crit);

        if (results.size() == 0) {
            return null;
        }

        return results.get(0);
    }
}
